package cn.edu.sgu.www.mhxysy.controller.zuoqi;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.zuoqi.ZuoqiDTO;
import cn.edu.sgu.www.mhxysy.entity.zuoqi.Zuoqi;
import cn.edu.sgu.www.mhxysy.pager.zuoqi.ZuoqiPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.zuoqi.ZuoqiService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "坐骑管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/zuoqi", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ZuoqiController {

	private final ZuoqiService zuoqiService;

	@Autowired
	public ZuoqiController(ZuoqiService zuoqiService) {
		this.zuoqiService = zuoqiService;
	}

	@ApiOperation("添加坐骑")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ZuoqiDTO zuoqiDTO) {
		zuoqiService.insert(zuoqiDTO);
		
		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID删除坐骑")
	@RequestMapping(value = "/deleteById", method = RequestMethod.POST)
	public JsonResult<Void> deleteById(
			@ApiParam(name = "id", value = "坐骑ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		zuoqiService.deleteById(id);

		return JsonResult.success(CustomMessages.DELETE_SUCCESS);
	}

	@ApiOperation("通过ID修改坐骑信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ZuoqiDTO zuoqiDTO) {
		zuoqiService.updateById(zuoqiDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}
	
	@ApiOperation("查询全部坐骑")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<Zuoqi>> selectAll() {
		List<Zuoqi> list = zuoqiService.selectAll();

		return JsonResult.success(null, list);
	}
	
	@ApiOperation("通过ID查询坐骑信息")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<Zuoqi> selectById(
			@ApiParam(name = "id", value = "坐骑ID", required = true)
			@NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
		Zuoqi zuoqi = zuoqiService.selectById(id);

		return JsonResult.success(null, zuoqi);
	}

	@ApiOperation("分页查询坐骑列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.POST)
	public JsonResult<JsonPage<Zuoqi>> selectByPage(ZuoqiPager pager) {
		Page<Zuoqi> result = zuoqiService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

}